﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.Common/MsgPackHelper
* 类       名: MsgPackHelper
* 功      能:  TD
* 时      间:  2024-05-15 16:40:12
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WaferSelectV3.Common
{
    public class MsgPackHelper
    {
        #region 2. 视觉模块通信相关

        // 2.1 回复内容(所有的内容回复)
        /// <summary>
        /// PC号，长度，相机编号，流水号，时间，OK，结束码
        //  PC5,13,1,21,20230207010321,OK,End//回复后隐裂的OK
        /// </summary>
        /// <returns></returns>
        public static byte[] GetOkResponseMsgSend(string pcName, string cameraNumber, int waferId)
        {
            byte[] message = Encoding.ASCII.GetBytes($"{pcName},{13},{cameraNumber},{waferId},{DateTimeHelper.GetDateTimeNowStr()},OK,End");
            return message;
        }

        // 2.2 错误码消息回复
        /// <summary>
        /// 收到错误码之后的回复消息
        /// 1: 3D 工位误触发: PC号,长度,相机号,流水号,时间戳,E0,End
        // 2:连片:          PC号,长度,相机号,流水号,时间戳,E1,End
        // 3:反向:          PC号,长度,相机号,流水号,时间戳,E2,End
        //4:遮挡:          PC号,长度,相机号,流水号,时间戳,E3,End
        //5:碎片:          PC号,长度,相机号,流水号,时间戳,E4,End
        //6:错位:          PC号,长度,相机号,流水号,时间戳,E5,End
        //7:PLC通讯异常:   PC号,长度,相机号,流水号,时间戳,E6,End
        //8:3D数据错乱:   PC号,长度,相机号,流水号,时间戳,E7,End
        /// </summary>
        /// <param name="msgHeader"></param>
        /// <param name="cameraNumber"></param>
        /// <param name="waferId"></param>
        /// <param name="errorType"></param>
        /// <returns></returns>
        public static byte[] GetErrorCodeResponseMsgSend(string msgHeader, string cameraNumber, int waferId, string errorType)
        {
            int msgLength = 13; // 消息长度
            string responseCode;
            switch (errorType)
            {
                case "00":
                    responseCode = "E0";
                    break;
                case "01":
                    responseCode = "E1";
                    break;
                case "02":
                    responseCode = "E2";
                    break;
                case "03":
                    responseCode = "E3";
                    break;
                case "04":
                    responseCode = "E4";
                    break;
                case "05":
                    responseCode = "E5";
                    break;
                case "06":
                    responseCode = "E6";
                    break;
                case "07":
                    responseCode = "E7";
                    break;
                default:
                    responseCode = "E8";
                    break;
            }
            byte[] message = Encoding.ASCII.GetBytes($"{msgHeader},{msgLength},{cameraNumber},{waferId},{DateTimeHelper.GetDateTimeNowStr()},{responseCode},End");
            return message;
        }

        // 发送消息给视觉模块
        // PC0,13,0,0,20230619085050,Clear,End
        public static byte[] GetDataClearMsgSend(string msgHeader)
        {
            string cameraName = "0";
            byte[] message = Encoding.ASCII.GetBytes($"{msgHeader},{13},{cameraName},{0},{DateTimeHelper.GetDateTimeNowStr()},Clear,End");
            return message;
        }

        // 发送晶棒编号给PC
        public static byte[] GetCrystalMsgSend(string pcName, string cameraName, string currentCrystalNumber)
        {
            pcName = "PC6";
            byte[] message = Encoding.ASCII.GetBytes($"PC6,13,0,0,{DateTimeHelper.GetDateTimeNowStr()},{currentCrystalNumber},End");
            return message;
        }

        // 发送3D标定给视觉
        public static byte[] GetCalibration3DMsgSend()
        {
            byte[] message = Encoding.ASCII.GetBytes($"PC3,13,0,0,{DateTimeHelper.GetDateTimeNowStr()},Correct,End");
            return message;
        }
        #endregion
    }
}
